const express=require('express')
const router=express.Router()
const pool=require('../pool');
const query = require('../router/utils')
const {   
    resSuccessData,
    resSuccess,
    resError} = require('../utils/index')
const {host} = require('./host')

router.get('/',(req,res)=>{
    pool.query('select * from zhuanji_c',(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+'/'+el.photo
        });
      res.send(result);
    })
})
router.get('/list',(req,res)=>{
    let id=req.query.id
    let page=req.query.page
    let count=6
    let start=(parseInt(page)-1)*count
    let sql=`select * from zhuanji  where z_id=? limit ${start},${count} `
    pool.query(sql,[id],(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+el.photo
        });
        res.send(result)
    })
})
router.get('/detail',(req,res)=>{
    let id=req.query.id
    let sql=`select * from zhuanji  where id=? `
    pool.query(sql,[id],(err,result)=>{
        if(err) throw err
        result.forEach(el => {
            el.photo=host+"/"+el.photo
        });
        res.send(result)
    })
})
//新增专辑
router.post('/add',async(req,res)=>{
    // 专辑分类的id 
    // song
    //photo
    let {
        zId,
        song,
        photo
    } = req.body;
    const obj = {
        zId,
        song,
        photo
    }
    //执行sql语句
    try{
         Object.keys(obj)
        .forEach(key=>{
            if(!obj[key]){
                resError(res,`${key}不能为空`);
                throw new Error('')
            }
        })
    }catch(e){
        console.log(e,'e');
        return
    }
   //sql语句
   let sql = `insert into zhuanji (z_id,song,photo) values (?,?,?)`;
   const ret = await query(sql,Object.values(obj));

   if(ret.affectedRows>0){
       obj.id = ret.insertId;
      resSuccessData(res,obj)

   }else{
    resError(res,'插入失败');
   }
    
})

//删除专辑接口
router.delete('/',async(req,res)=>{
    // 传入数组
    let dels = req.body;
    // console.log(dels)
    if(dels.length){
        dels = dels.join(',');
        // 删除
        let sql = `delete from zhuanji where id in(${dels})`;
        const ret = await query(sql,[]);
        if(ret.affectedRows>0){
            resSuccess(res)
        }else{
            resError(res,'删除失败')
        }
    }else{
        resError(res,'删除参数不能为空')
    }
})
//修改
router.put('/',async(req,res)=>{
    // 分类中文名称 英文名称
    let {
       zId, 
       song,
       photo,
       id
   } = req.body;

   //空判
   const obj = {
        zId,
       song,
       photo,
       id
   };
   //空判
   try{
       Object.keys(obj)
       .forEach(key=>{
           if(!obj[key] && (key=='id' || key=='zId')){
               resError(res,`${key}不能为空`);
               throw new Error('')
           }
       })
   }catch(e){
       console.log(e,'zhuanji');
       return
   }
   //sql语句
   let sql = `update zhuanji set z_id=?,song=?,photo=? where id=?`;
   const ret = await query(sql,Object.values(obj));
   if(ret.affectedRows>0){
       resSuccessData(res,obj)

   }else{
    resError(res,'修改失败');
   }
})
module.exports=router